package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Book;
import models.Borrow;
import models.Indent;
import models.LendCard;
import models.LendCardF;
import models.MemType;
import models.Member;
import models.UserCar;
import play.mvc.Controller;
import play.mvc.Scope.Session;
import service.BookService;
import service.LendCardService;
import service.OrderService;
import util.Util;
public class Order extends Controller{

	public static void order3(String adress,String username,String mobie,String tel,String cityt){
		String script = "";
		Indent o = new Indent();
		String userid = session.get("userid");
		List<UserCar> cars = UserCar.find("userid=? and type=0 ", userid).fetch();
		
		if(userid == null || "".equals(userid)){
			script = "aduAlert('你还未登录,请先登录后在操作!','提示',function(e){window.location='/controllers/MemberView.log'})";
			render("/501.html",script);
		}
		Member user = Member.findById(Long.parseLong(userid));
		if(user == null || "".equals(user)){
			script = "aduAlert('你还未登录,请先登录后在操作!','提示',function(e){window.location='/controllers/MemberView.log'})";
			render("/501.html",script);
		}
		if(cars == null){
			script = "aduAlert('你的借阅车中还没有书籍,请先去选择将要借阅的书籍在操作!','提示',function(e){window.location='/books.html'})";
			render("/501.html",script);
		}
		if(cars.size() <= 0){
			script = "aduAlert('你的借阅车中还没有书籍,请先去选择将要借阅的书籍在操作!','提示',function(e){window.location='/books.html'})";
			render("/501.html",script);
		}
		//判读角色的等级是否可以借这么多的书
		int hky = 0;
		try{
			user = Member.findById(Long.parseLong(userid));
			LendCard card = LendCard.find("lecnum=?",user.lecnum).first();
			double m = card.lecmone;
			LendCardF f = LendCardService.getLevel(m);
			//得到会员的当前在借出状态的图书
			int booCount = OrderService.getBooCount(userid);
			
			int carSize = 0;
			try{
				for(UserCar c : cars){
					carSize += c.count;
				}
			}catch(Exception e){}
			//f.bookSize-booCount会员还可以借的图书总数
			hky = f.bookSize-booCount;
			if(carSize > hky){
				throw new Exception("level  -------order2");
			}
		}catch(Exception e){
			System.err.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			e.printStackTrace();
			System.err.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			script = "aduConfirm('你的等级不够借阅当前数量的书籍!','提示',function(e){if(e){window.location='/controllers/MemberView.paymoney';}else{" +
					 "window.history.go(-1);}},{okBut:'去升级',cancelBut:'回借阅车'})";
			render("/501.html",script);
		}
		
		o.indnum = Util.uuid();
		o.indsta = 0;
		o.indaddr = adress;
		o.indcon = username;
		o.indmob = mobie;
		o.indtel = tel;
		// o.indcity = province+"  "+city+"  "+county;
		o.indcity = cityt;
		o.memid = userid;
		o.cdate = new Date();
		
		o.save();
		try{
			for(UserCar car : cars){
				Borrow bor = new Borrow();
				bor.indent = o;
				bor.boramo = car.count;
				bor.bortime = o.cdate;
				bor.booid = car.booid+"";
				bor.borsta = 1;//1刚下单订单,还未发货,0已经归还,2书在手上,3申请归还中
				bor.borsbt = new Date();
				bor.save();
			}
		}catch(Exception e){
			e.printStackTrace();
			o.delete();
		}
		
		//订单成功清空借阅车
		for(UserCar car : cars){
			try{
				car.delete();
			}catch(Exception e){}
		}
		render("Order/order4.html",o);
	}
	
	public static void order2(){
		String userid = session.get("userid");
		List<UserCar> bookses = UserCar.find("userid=? and type=0 ", userid).fetch();
		
		int hky = 0;
		
		Member user = null;
		String script="";
		try{
			user = Member.findById(Long.parseLong(userid));
			LendCard card = LendCard.find("lecnum=?",user.lecnum).first();
			double m = card.lecmone;
			LendCardF f = LendCardService.getLevel(m);
			//得到会员的当前在借出状态的图书
			int booCount = OrderService.getBooCount(userid);
			
			int carSize = 0;
			try{
				List<UserCar> cars = UserCar.find("userid=? and type=0 ", userid).fetch();
				for(UserCar c : cars){
					carSize += c.count;
				}
			}catch(Exception e){}
			//f.bookSize-booCount会员还可以借的图书总数
			hky = f.bookSize-booCount;
			if(carSize > hky){
				throw new Exception("level  -------order2");
			}
		}catch(Exception e){
			System.err.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			e.printStackTrace();
			System.err.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			script = "aduConfirm('你的等级不够借阅当前数量的书籍!','提示',function(e){if(e){window.location='/controllers/MemberView.paymoney';}else{" +
					 "window.history.go(-1);}},{okBut:'去升级',cancelBut:'回借阅车'})";
			render("/501.html",script);
		}
		render("Order/order2.html",bookses,user);
	}
	
	public static void removeBook(Long booid){
		String userid = session.get("userid");
		UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
		car.delete();
	}
	//修改购物车
	public static void editCar(Long booid,int size){
		String userid = session.get("userid");
		UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
		
		Member user = null;
		try{
			user = Member.findById(Long.parseLong(userid));
			LendCard card = LendCard.find("lecnum=?",user.lecnum).first();
			double m = card.lecmone;
			LendCardF f = LendCardService.getLevel(m);
			//得到会员的当前在借出状态的图书
			int booCount = OrderService.getBooCount(userid);
			
			int carSize = 0,hky=0;
			try{
				List<UserCar> cars = UserCar.find("userid=? and type=0 ", userid).fetch();
				for(UserCar c : cars){
					carSize += c.count;
				}
			}catch(Exception e){}
			//f.bookSize-booCount会员还可以借的图书总数
			hky = f.bookSize-booCount;
			if(carSize+size > hky){
				throw new Exception("level  -------order2");
			}
		}catch(Exception e){
			render(0);			
		}
		
		if(car == null){
			car = new UserCar();
			car.booid = booid;
			car.userid = userid;
			car.count = 1;
			car.type = 0;
		}else{
			car.count = size;
		}
		car.save();
	}
	//显示购物车
	public static void showCar(){
		String userid = session.get("userid");
		List<UserCar> cars = UserCar.find("userid=? and type=0 ", userid).fetch();
		List<UserCar> scj = UserCar.find("userid=? and type=1 ", userid).fetch();
		
		int hky = 0;
		Member user = null;
		try{
			user = Member.findById(Long.parseLong(userid));
			LendCard card = LendCard.find("lecnum=?",user.lecnum).first();
			double m = card.lecmone;
			LendCardF f = LendCardService.getLevel(m);
			//得到会员的当前在借出状态的图书
			int booCount = OrderService.getBooCount(userid);
			//f.bookSize-booCount会员还可以借的图书总数
			hky = f.bookSize-booCount;
		}catch(Exception e){ }
		
		render("/Order/order1.html",cars,scj,hky);
	}
	//添加图书
	public static void addBook(Long booid){
		String userid = session.get("userid");
		UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
		
		Member user = null;
		try{
			user = Member.findById(Long.parseLong(userid));
			//获取会员的借阅卡
			LendCard card = LendCard.find("lecnum=?",user.lecnum).first();
			double m = card.lecmone;
			//得到会员的借阅卡等级
			LendCardF f = LendCardService.getLevel(m);
			int booCount = OrderService.getBooCount(userid);
			int carSize = 0,hky=0;
			try{
				List<UserCar> cars = UserCar.find("userid=? and type=0 ", userid).fetch();
				for(UserCar c : cars){
					carSize += c.count;
				}
			}catch(Exception e){}
			//f.bookSize-booCount会员还可以借的图书总数
			hky = f.bookSize-booCount;
			if(carSize > hky){
				throw new Exception("level  -------order2");
			}
		}catch(Exception e){
			int succ = 0;
			render("/502.html",succ);			
		}
		
		if(car == null){
			car = new UserCar();
			car.booid = booid;
			car.userid = userid;
			car.count = 1;
			car.type = 0;
		}else{
			car.count = car.count+1;
		}
		car.save();
	}
	
	/**
	 * 获取购物车中的总数量
	 * @param session 
	 * @param booid 不计算当前id
	 * @return
	 */
	public static int getCarSize(Long booid,String userid){
		int count = 0;
		try{
			UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
			count = car.count;
		}catch(Exception e){}
		return count;
	}
	
	public static void scBook(Long booid){
		String succ="";
		String userid = session.get("userid");
		UserCar car = UserCar.find("userid=? and type=1 and booid=?", userid,booid).first();
		if(car == null){
			car = new UserCar();
			car.booid = booid;
			car.userid = userid;
			car.count = 1;
			car.type = 1;
			car.save();
			succ = "收藏成功";
		}else{
			succ = "书籍已经存在收藏夹中";
		}
		render("/502.html",succ);
	}
	
	public static void addScBook(Long booid){
		String userid = session.get("userid");
		UserCar sc = UserCar.find("userid=? and type=1 and booid=?", userid,booid).first();
		UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
		
		if(sc == null){
			car.type = 1;
			car.save();
		}else{
			car.delete();
		}
	}
	
	public static void jrCarBook(Long booid){
		String userid = session.get("userid");
		UserCar sc = UserCar.find("userid=? and type=1 and booid=?", userid,booid).first();
		UserCar car = UserCar.find("userid=? and type=0 and booid=?", userid,booid).first();
		if(car == null){
			sc.type = 0;
			sc.save();
		}else{
			car.count += sc.count;
			car.save();
			sc.delete();
		}
	}
	
	
	//还书 ------------
	public static void jdsList(){
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("/proscenium/login.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		//读取会员的所有订单
		List<Indent> os = Indent.find("memid=?", meb.id+"").fetch();
		String oids = "";
		for(Indent o : os){
			oids += ","+o.id;
		}
		
		List<Borrow> jss = null;
		if(oids != null && oids.trim().length() > 1){
			jss = Borrow.find("indent.id in ("+oids.substring(1)+") and borsta not in (0,1) order by borsta").fetch();
		}
		render("/proscenium/wyhs.html",meb,memtype,jss);
	}
	
}
